package com.goalias.job;

import com.goalias.constant.RedisConstants;
import com.goalias.constant.SystemConstants;
import com.goalias.domain.entity.Article;
import com.goalias.service.IArticleService;
import com.goalias.service.RedisService;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Component
public class RedisJob {

    @Resource
    private IArticleService articleService;

    @Resource
    private RedisService redisService;

    @Scheduled(cron = "0 0/1 * * * ?")
    public void updateViewCount() {
        Map<String, Object> viewCountMap = redisService.hmGet(RedisConstants.ARTICLE_VIEW_COUNT_KEY);
        List<Article> articles = viewCountMap.entrySet().stream()
                .map(entry -> new Article(Long.valueOf(entry.getKey()), (Long)entry.getValue()))
                .collect(Collectors.toList());

        articleService.updateBatchById(articles);
    }
}
